package com.pocketpets.task;

import java.util.Map;

import android.app.ProgressDialog;
import android.content.Context;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;

import com.google.android.maps.GeoPoint;
import com.pocketpets.globaldata.ItemListClass;
import com.pocketpets.globaldata.LocationData;
import com.pocketpets.model.map.SpotDbAdapter;
import com.pocketpets.ui.map.AttractionLayerTw;
import com.pocketpets.ui.map.AttractionSpot;

public class LoadItemFromDatabaseTask extends AsyncTask<Map<String,Object>,Integer,AttractionLayerTw>{
	private static SpotDbAdapter mDbHelper;
	ProgressDialog dialog;
	
	private Handler handler = null;

	public LoadItemFromDatabaseTask(Handler h) {
		this.handler = h;
	}
	
	@Override
	protected void onPreExecute()
	{
		handler.sendEmptyMessage(0);
	}
	@Override
	protected AttractionLayerTw doInBackground(Map... map) {
		Context mContext=(Context) map[0].get("Context");
		AttractionLayerTw attLayer=(AttractionLayerTw) map[0].get("AttractionLayer");
		mDbHelper = new SpotDbAdapter(mContext);
        mDbHelper.open();
        // Get a cursor to all the locations in the database
        Cursor spotCursor = mDbHelper.fetchAllSpotinfo();
        //startManagingCursor(spotCursor);
        // Move to the first row in the cursor
        spotCursor.moveToFirst();
        do{ // for all the rows in the cursor
        // Get the spot's name, latitude and longitude
        LocationData itemStruct=new LocationData();
        itemStruct.setTitle(spotCursor.getString(spotCursor.getColumnIndexOrThrow(SpotDbAdapter.KEY_SPOT)));
        itemStruct.setLat(spotCursor.getDouble(spotCursor.getColumnIndexOrThrow(SpotDbAdapter.KEY_LATITUDE)));
        itemStruct.setLng(spotCursor.getDouble(spotCursor.getColumnIndexOrThrow(SpotDbAdapter.KEY_LONGITUDE)));
        itemStruct.setPic(spotCursor.getString(spotCursor.getColumnIndexOrThrow(SpotDbAdapter.KEY_PIC)));
        ItemListClass.addToItemList(itemStruct);
//        	String spot =
//        spotCursor.getString(spotCursor.getColumnIndexOrThrow(SpotDbAdapter.KEY_SPOT));
//        double latitude =
//        spotCursor.getDouble(spotCursor.getColumnIndexOrThrow(SpotDbAdapter.KEY_LATITUDE));
//        double longitude =
//        spotCursor.getDouble(spotCursor.getColumnIndexOrThrow(SpotDbAdapter.KEY_LONGITUDE));
//        String coordinates = String.format("Longitude: %1$s\nLatitude: %2$s",longitude, latitude);
//        String pic=spotCursor.getString(spotCursor.getColumnIndexOrThrow(SpotDbAdapter.KEY_PIC));
        // Geopoint refers to the location. Need to specify latitude,
        // longitude in microdegrees
        GeoPoint point = new GeoPoint((int)(itemStruct.getLat()*1e+6),(int)(itemStruct.getLng()*1e+6));
        // Create an overlay item for the spot
        AttractionSpot overlayitem = new AttractionSpot(point,itemStruct.getTitle(),itemStruct.getPic(),0);
        // Add the overlay item into the HelloItemizedOverlay for each spot
        attLayer.addOverlay(overlayitem);
        } while (spotCursor.moveToNext());
     // until you exhaust all the rows. returns false when we reach the end of the cursor
       
        spotCursor.close();
		return attLayer;
	}

	protected void onPostExecute(AttractionLayerTw attrLayer)
	{
		super.onPostExecute(attrLayer);
		Message msg=new Message();
        msg.obj=attrLayer;
        handler.sendMessage(msg);

		
	}


	

}
